﻿using System.Runtime.InteropServices;

class T1{
    public enum T1SlvType
    {
        None = 0,
        ServoMotor,
        Picker4,
        IO32,
        RackIO,
    }

    public enum PortNo
    {
        PORT0 = 0,
        PORT1,
        PORT2,
    }

     public const int MaxSlvNum = 14;

    // T1WinDrv가 동작하는 지를 알려주는 함수이다. 메카트로링크 통신을 하려면 언제
	//나 T1WinDrv가 먼저 실행되어 있어야 한다.
	[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern bool IsRunDrv();
	
	//T1WinDrv가 메모리 통신을 하기 위해서 메모리맵 파일을 연결한다. 이 함수를 통해서
	//메모리맴과 연결이 되어야만 다른 모든 함수들의 사용이 가능합니다.
	//0x0000 OK 0x0008 T1WinDrv 미 실행 0x0002 PC 문제 끄고 다시 시작
	[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern uint ConnectDrv();


    [DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void ClearSlvType();
    [DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void SetSlvType(short ushPort, short ushMt, char uchType);
    [DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern uint StartDrv2();
	/*
	슬레이브 정보 배열 입력 0번 PORT
	UCHAR uchSlvType[MAX_MASTER_NUM][MAX_SLAVE_NUM];
	uchSlvType[0][0]=1; // 서보모터
	uchSlvType[0][1]=3; // DAISY32입력
	uchSlvType[0][2]=3; // DAISY32출력
	uchSlvType[0][3]=1; // 서보모터
	uchSlvType[0][4]=4; // DAISY128(입력모듈2개, 촐력모듈3개)
	uchSlvType[0][5]=1; // 서보모터
	UINT nRet = StartDrv(uchSlvType);
	 */
	//[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern uint StartDrv(ref sbyte[,] puchSlvType);

	//메카트로링크 연결상태 값을 반환하는 함수
	//[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern short GetSlvType(ref short pErr);
    [DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void MotionUpdate(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern long GetRealCount(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern long GetLatchCount(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void SetRealCount(short ushPort, short ushMt, long lData);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void SetMonitor2nd(short ushPort, short ushMt, char uchMonCode);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern char GetMonitor2ndCode(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern long GetMonitor2nd(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern char GetAlarmCode(short ushPort, short ushMt);

	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsALM(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsWARNG(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsCMDRDY(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsSVON(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsPON(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsMLOCK(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsZPOINT(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsPSET(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsDEN(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsT_LIM(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsL_CMP(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsNEAR(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsP_SOT(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsN_SOT(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsP_OT(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsN_OT(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsDEC(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsPA(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsPB(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsPC(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsEXT1(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsEXT2(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsEXT3(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsBRK(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsIO12(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsIO13(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsIO14(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool IsIO15(short ushPort, short ushMt);

	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetPosRefFilter(short ushPort, short ushMt, short ushPosRefFilter);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetGainSwitch(short ushPort, short ushMt, short ushGainSw);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetSpeedLoopControl(short ushPort, short ushMt, short ushPPI);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	ClearPosLoopIntegral(short ushPort, short ushMt, short ushP_PI_CLR);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	EnableFwdTorqueLimit(short ushPort, short ushMt, bool bP_CL);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	EnableRevTorqueLimit(short ushPort, short ushMt, bool bN_CL);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	EnableSCurve(short ushPort, short ushMt, bool bOn);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetSCurveTime(short ushPort, short ushMt, short ushTime);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetAccelDecel(short ushPort, short ushMt, 
									short ushAccStep1, short ushAccStep2, short ushAccSwVal, 
									short ushDecStep1, short ushDecStep2, short ushDecSwVal);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetExpFilterBias(short ushPort, short ushMt, short ushBias);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetExpFilter(short ushPort, short ushMt, short ushTime);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	SetMovAvrFilter(short ushPort, short ushMt, short ushTime);

	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern void	MtDataReset(short ushPort, short ushMt);
	
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	NOP(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	PRM_RD(short ushPort, short ushMt, short No, char Size);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	PRM_WR(short ushPort, short ushMt, short No, char Size, long Parameter);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	CONFIG(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	ALM_CLR(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	SYNC_SET(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	POS_SET(short ushPort, short ushMt, char PsSubCmd, long PosData);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	PPRM_WR(short ushPort, short ushMt, short No, char Size, long Para);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	BRK_ON(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	BRK_OFF(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	SENS_ON(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	SENS_OFF(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	HOLD(short ushPort, short ushMt, char StopMode);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	LTMOD_ON(short ushPort, short ushMt, char LtSgn);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	LTMOD_OFF(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	SERVO_ON(short ushPort, short ushMt);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	SERVO_OFF(short ushPort, short ushMt);

	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	INTERPOLATE(short ushPort, short ushMt, long TPos, long Vff);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	POSING(short ushPort, short ushMt, long TPos, long TSpd);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	FEED(short ushPort, short ushMt, long TSpd);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	LATCH(short ushPort, short ushMt, int LtSgn, long TPos, long Vff);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	EX_POSING(short ushPort, short ushMt, int LtSgn, long TPos, long TSpd);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	ZRET(short ushPort, short ushMt, int LtSgn, long TSpd);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	VELCTRL(short ushPort, short ushMt, short PTLim, short NTLim, long VRef);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	TRQCTRL(short ushPort, short ushMt, long VLim, long TqRef);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	ADJ(short ushPort, short ushMt, char CCmd,short CAddress,long CData);
	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern bool	SMON(short ushPort, short ushMt);

	[DllImport("c:\\Windows\\System32\\T1lib.dll")] public static extern uint	DualPOSING(short ushPort, short ushMt1, short ushMt2, long RelatePos, long TSpd);

	/*DAISY-R128 랙 타입 I/O 모듈 사용 시에 해당 모듈에 입력과 출력 모듈이 몇 개씩 장
	착되어 있는지를 설정하는 함수.
	▶ ushPort : 원하는 서보드라이버의 포트 값.LILY-101이면 언제나 0으로 설정 LILY-301이면 0,1,2으로 설정.
	▶ ushMt : 원하는 서보드라이버의 ID 값.
	▶ nInModuleNum : 입력 모듈 개수. (0~8개)
	▶ nOutModuleNum : 출력 모듈 개수.(0~8개)
	 */
	[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern void SetRackIOType(short ushPort, short ushSlv, int nInModuleNum, int nOutModuleNum);

	//DAISY-RACK128에서 입력 모듈 사용시, 해당 모듈의 입력 값을 읽어오는 함수
	[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern short RackReadInputData(short ushPort, short ushSlv, int ModuleNum);

	//DAISY-RACK128에서 출력 모듈 사용시, 해당 모듈의 출력 값을 써주는 함수.
	[DllImport("C:\\WINDOWS\\system32\\T1Lib.dll")] public static extern void RackWriteOutputData(short ushPort, short ushSlv, int ModuleNum, short ushData16);
}